import emitter from "../emitter";
import { planeBullet } from "../gameMrg";

const { ccclass, property } = cc._decorator;

@ccclass
export default class enemy_bullet extends cc.Component {
    onEnable() {
        this.initData();
        this.addEventListener();
    }

    @property(cc.Node)
    nodeBullet: cc.Node = null;
    addEventListener() {
    }

    @property(cc.Integer)
    speed = 0;
    minPosi_y;
    initData() {
        emitter.emit("resetPlayerPosi", this.setEndPosi.bind(this));
        this.minPosi_y = -(cc.winSize.height / 2 + 500);
    }

    isTrack = false;
    setIsTrack(isTrack) {
        this.isTrack = isTrack;
    }

    posData;
    setEndPosi(posi) {
        let endPosi = this.node.parent.convertToNodeSpaceAR(posi);
        if (endPosi.y >= this.node.y) endPosi.y = this.node.y - 400;
        let disPos = endPosi.normalizeSelf();
        disPos.divSelf(this.speed);
        this.posData = disPos;
    }

    update() {
        if (this.isTrack) {
            if (!this.posData) return
            this.node.setPosition(this.node.position.add(this.posData));
            if (this.node.y < this.minPosi_y || this.node.y > Math.abs(this.minPosi_y)) {
                this.posData = null;
                this.node.removeFromParent();
                emitter.emit("putEnemyBullet", this.node);
            }
        }
        else {
            this.nodeBullet.y -= 4;
            if (this.nodeBullet.y < this.minPosi_y) {
                this.posData = null;
                this.node.removeFromParent();
                emitter.emit("putEnemyBullet", this.node);
            }
        }
    }
}
